/*
 * Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.submarine.exchange;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @file org.submarine.exchange.Response.java
 *
 * 消息反馈包装类
 * 
 * @author gaojian
 * @created 2013-8-28 下午4:33:51
 * @version 0.0.1
 * @since 0.0.1
 * 
 */
public class Response {
	
	private final CountDownLatch countDownLatch = new CountDownLatch(1);
	
	/**
	 * 相应，对应请求ID
	 */
	private long requestId;
	
	/**
	 * 数据内容
	 */
	private Object data;
	
	/**
	 * 请求发送状态
	 */
	private boolean sendStatus;
	
	/**
	 * 响应状态
	 */
	private boolean responseStatus = false;
	
	private volatile Throwable cause;

	/**
	 * @return the requestId
	 */
	public long getRequestId() {
		return requestId;
	}

	/**
	 * @param requestId the requestId to set
	 */
	public void setRequestId(long requestId) {
		this.requestId = requestId;
	}

	/**
	 * @return the data
	 */
	public Object getData() {
		return data;
	}

	/**
	 * @param data the data to set
	 */
	public void setData(Object data) {
		this.data = data;
	}

	/**
	 * @return the sendStatus
	 */
	public boolean isSendStatus() {
		return sendStatus;
	}

	/**
	 * @param sendStatus the sendStatus to set
	 */
	public void setSendStatus(boolean sendStatus) {
		this.sendStatus = sendStatus;
	}
	
	/**
	 * @return the responseStatus
	 */
	public boolean isResponseStatus() {
		return responseStatus;
	}

	/**
	 * @param responseStatus the responseStatus to set
	 */
	public void setResponseStatus(boolean responseStatus) {
		this.responseStatus = responseStatus;
	}

	/**
	 * @return the cause
	 */
	public Throwable getCause() {
		return cause;
	}

	/**
	 * @param cause the cause to set
	 */
	public void setCause(Throwable cause) {
		this.cause = cause;
	}

	public boolean waitResponse(final long timeoutMillis) throws InterruptedException {
        this.countDownLatch.await(timeoutMillis, TimeUnit.MILLISECONDS);
        return this.responseStatus;
    }
	
	public void putData(Object data) {
		 this.data = data;
		 this.countDownLatch.countDown();
	}
	
}
